package com.huawei.crm.shiro;

import com.huawei.crm.domain.Employee;
import com.huawei.crm.mapper.EmployeeMapper;
import com.huawei.crm.mapper.PermissionMapper;
import com.huawei.crm.mapper.RoleMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("crmRealm")
public class CRMRealm extends AuthorizingRealm {

    @Autowired
    private EmployeeMapper mapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    // 从spring容器中找到我们配置的凭证匹配器,设置给当前的Reaml的credentialsMatcher属性
    @Autowired
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        super.setCredentialsMatcher(credentialsMatcher);
    }

    public void clearCached() {
        PrincipalCollection principals =
                SecurityUtils.getSubject().getPrincipals();
        super.clearCache(principals);
    }

    // 获取认证信息
    // authenticationToken:用户的账号密码信息对象
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 获取用户的账号
        String username = (String) authenticationToken.getPrincipal();
        // 根据用户的账号去数据库查询用户的真实信息
        Employee currentEmp = mapper.selectByName(username);
        if (currentEmp != null) {
            // currentEmp 作为身份对象使用,因为后面需要使用到当前登录的用户信息
            return new SimpleAuthenticationInfo(currentEmp, currentEmp.getPassword(), ByteSource.Util.bytes(currentEmp.getName()), "crmRealm");
        }
        return null;
    }

    // 获取授权信息
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        // 获取当前登录用户的角色和权限信息,封装到AuthorizationInfo对象中并返回

        // 如果当前用户是超级管理员,添加admin角色,和所有权限*:*
        Employee currentEmp = (Employee) principalCollection.getPrimaryPrincipal();
        if (currentEmp.isAdmin()) {
            info.addRole("admin");
            info.addStringPermission("*:*");
            return info;
        }
        // 如果不是超级管理员,查询到当前用户的角色和权限,完成授权
        // 查询当前用户的角色信息(sn)
        List<String> sns = roleMapper.selectSnByEmpId(currentEmp.getId());
        // 查询当前用户的权限信息(expression)
        List<String> expressions = permissionMapper.selectExpressionsByEmpId(currentEmp.getId());

        info.addRoles(sns);
        info.addStringPermissions(expressions);

        return info;
    }
}
